package rbk.rours.service.user;

import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import rbk.rours.common.dto.FestivalInfoDto;
import rbk.rours.common.entities.FestivalInfo;
import rbk.rours.common.utils.JsonResult;

@Service
public class FestivalService implements IFestivalService {

    @Autowired
    NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    ModelMapper modelMapper;

    @Autowired
    private FestivalRepository festivalRepository;

    @Override
    public JsonResult AddFestival(FestivalInfoDto request) throws Exception {
        FestivalInfo f = modelMapper.map(request, FestivalInfo.class);
        festivalRepository.save(f);
        return JsonResult.ok(f);
    }

    @Override
    public JsonResult UpdateFestival(FestivalInfoDto request) throws Exception {
        FestivalInfo f = modelMapper.map(request, FestivalInfo.class);
        festivalRepository.save(f);
        return JsonResult.ok(f);
    }

    @Override
    public JsonResult GetFestival(String id) throws Exception {
        FestivalInfo f = festivalRepository.findOne(Long.parseLong(id));
        if (f != null)
            return JsonResult.ok(f);
        else
            return JsonResult.errorMsg("未找到该数据");
    }

    @Override
    public JsonResult GetFestivalList() throws Exception {
        List<FestivalInfo> fList = festivalRepository.findAll();
        if (fList != null)
            return JsonResult.ok(fList);
        else
            return JsonResult.errorMsg("未找到数据");
    }

    @Override
    public JsonResult DeleteFestival(String id) throws Exception {
        FestivalInfo f = festivalRepository.findOne(Long.parseLong(id));
        if (f != null) {
            festivalRepository.delete(f);
            return JsonResult.ok("删除成功");
        } else
            return JsonResult.errorMsg("未找到该数据");
    }

}
